Browse Source

fix(Exploration): resume chapter exploration after battle rewards

Recognize chapter controls on a single frame and recheck the world map before resetting search progress. Limit the vertical search for the map marker so chapter roof tiles do not match.

Replay the reported roof crop and cover scene changes between battle settlement and the next chapter action.
unknown 4 days ago
parent
commit
cf4d3afc7a

+ 26 - 31
tasks/Exploration/base.py

@@ -67,22 +67,31 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
         logger.info(f'Swipe end area diff: {diff:.2f}')
         return diff <= threshold
 
+    def _in_chapter(self) -> bool:
+        return (self.appear(self.I_E_SETTINGS_BUTTON)
+                or self.appear(self.I_E_AUTO_ROTATE_ON)
+                or self.appear(self.I_E_AUTO_ROTATE_OFF))
+
     def get_current_scene(self, reuse_screenshot: bool = True) -> Scene:
         if not reuse_screenshot:
             self.screenshot()
 
-        if self.appear(self.I_CHECK_EXPLORATION) and not self.appear(self.I_E_SETTINGS_BUTTON):
-            return Scene.WORLD
-        elif self.appear(self.I_E_EXPLORATION_CLICK):
+        # 结算过渡时章节背景可能命中大地图标识,优先看同一帧的入口和章节控件。
+        if self.appear(self.I_E_EXPLORATION_CLICK):
             return Scene.ENTRANCE
-        elif self.appear(self.I_E_SETTINGS_BUTTON) or self.appear(self.I_E_AUTO_ROTATE_ON) or self.appear(self.I_E_AUTO_ROTATE_OFF):
+        elif self._in_chapter():
             return Scene.MAIN
-        elif self.is_in_prepare():
+        elif self.appear(self.I_CHECK_EXPLORATION):
+            return Scene.WORLD
+        elif self.is_in_prepare(is_screenshot=False):
             return Scene.BATTLE_PREPARE
-        elif self.is_in_battle():
+        elif self.is_in_battle(is_screenshot=False):
             return Scene.BATTLE_FIGHTING
-        elif self.is_in_room() or self.appear(self.I_CREATE_ENSURE):
+        elif self.is_in_room(is_screenshot=False) or self.appear(self.I_CREATE_ENSURE):
             return Scene.TEAM
+        # 收起章节列表时,大地图标识可能随面板隐藏。仅在没有其他场景控件时使用箭头。
+        elif self.appear(self.I_EXP_ARROW_LEFT):
+            return Scene.WORLD
 
         logger.info("Unknown scene")
         return Scene.UNKNOWN
@@ -408,7 +417,7 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
             if self.appear(self.I_UI_BACK_YELLOW) and self.appear(self.I_E_EXPLORATION_CLICK):
                 break
             # 探索大世界界面
-            if self.appear(self.I_CHECK_EXPLORATION) and not self.appear(self.I_E_SETTINGS_BUTTON):
+            if self.appear(self.I_CHECK_EXPLORATION) and not self._in_chapter():
                 break
   
             # 防止BOSS打完箱子刚落地,脚本就手快点退出了
@@ -433,12 +442,7 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
                 # https://github.com/runhey/OnmyojiAutoScript/issues/1935
                 if not self.appear(self.I_BACK_YOLLOW) or self.appear(self.I_E_EXPLORATION_CLICK):
                     continue
-                is_main = (
-                        self.appear(self.I_E_SETTINGS_BUTTON)
-                        or self.appear(self.I_E_AUTO_ROTATE_ON)
-                        or self.appear(self.I_E_AUTO_ROTATE_OFF)
-                )
-                if is_main and self.click(self.I_BACK_YOLLOW, interval=3.5):
+                if self._in_chapter() and self.click(self.I_BACK_YOLLOW, interval=3.5):
                     continue
             
             if self.appear(self.I_EXPLORATION_TITLE) or self.appear(self.I_CHECK_EXPLORATION):
@@ -454,9 +458,7 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
     def fire(self, button) -> bool:
         self.ui_click_until_disappear(button, interval=3)
         self.screenshot()
-        if (self.appear(self.I_E_SETTINGS_BUTTON) or
-                self.appear(self.I_E_AUTO_ROTATE_ON) or
-                self.appear(self.I_E_AUTO_ROTATE_OFF)):
+        if self._in_chapter():
             # 如果还在探索说明,这个是显示滑动导致挑战按钮不在范围内
             logger.warning('Fire button disappear, but still in exploration')
             return False
@@ -465,20 +467,13 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
         return True
 
     def wait_world_stable(self) -> bool:
-        """
-        # 打开右边箭头 and https://github.com/runhey/OnmyojiAutoScript/pull/1589/
-        https://github.com/runhey/OnmyojiAutoScript/issues/1588
-        @return:
-        """
-        while 1:
-            scene = self.get_current_scene(reuse_screenshot=False)
-            if scene == Scene.WORLD and self.appear(self.I_EXP_ARROW_RIGHT):
-                return True
-            if scene == Scene.ENTRANCE:
-                logger.warning('World scene unstable, possibly transient frame after paper doll collection')
-                return False
-            if self.appear_then_click(self.I_EXP_ARROW_LEFT, interval=2):
-                continue
+        """用新截图确认章节列表已展开;切场或展开后交回主循环重新判断。"""
+        if self.get_current_scene(reuse_screenshot=False) != Scene.WORLD:
+            return False
+        if self.appear(self.I_EXP_ARROW_RIGHT):
+            return True
+        self.appear_then_click(self.I_EXP_ARROW_LEFT, interval=2)
+        return False
 
 
 if __name__ == "__main__":

+ 4 - 6
tasks/Exploration/solo.py

@@ -136,12 +136,11 @@ class SoloExploration(BaseExploration):
             logger.info(f'[run_solo] Current scene: {scene.name}')  # TODO 2026.06.22 之后删掉这个刷屏的
             #
             if scene == Scene.WORLD:
+                if not self.wait_world_stable():
+                    continue
                 self.explore_init = False
                 self._reset_search_state(reset_swipe_cnt=True)
                 self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
-                # 打开右边箭头
-                if not self.wait_world_stable():
-                    continue
                 if self.check_exit():
                     break
                 self.open_expect_level()
@@ -210,12 +209,11 @@ class SoloExploration(BaseExploration):
             scene = self.get_current_scene()
             # 探索大世界
             if scene == Scene.WORLD:
+                if not self.wait_world_stable():
+                    continue
                 self.explore_init = False
                 self._reset_search_state(reset_swipe_cnt=True)
                 self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
-                # 打开右边箭头
-                if not self.wait_world_stable():
-                    continue
                 if self.check_exit():
                     self.wait_until_stable(self.I_UI_CANCEL, timer=Timer(0.6, 2))
                     if self.appear(self.I_UI_CANCEL):

+ 1 - 1
tasks/GameUi/assets.py

@@ -31,7 +31,7 @@ class GameUiAssets:
 
 	# Image Rule Assets
 	# 探索界面标识
-	I_CHECK_EXPLORATION = RuleImage(roi_front=(1152,148,21,22), roi_back=(1138,134,56,50), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
+	I_CHECK_EXPLORATION = RuleImage(roi_front=(1152,148,21,22), roi_back=(1138,146,56,26), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
 	# 探索前往觉醒 
 	I_EXPLORATION_GOTO_AWAKE_ZONE = RuleImage(roi_front=(161,633,47,44), roi_back=(33,619,1150,99), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_exploration_goto_awake_zone.png")
 	# 探索前往御魂 

+ 1 - 1
tasks/GameUi/page/image_exploration.json

@@ -3,7 +3,7 @@
     "itemName": "check_exploration",
     "imageName": "page_check_exploration.png",
     "roiFront": "1152,148,21,22",
-    "roiBack": "1138,134,56,50",
+    "roiBack": "1138,146,56,26",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "探索界面标识"

+ 85 - 0
tests/tasks/Exploration/test_world_transition.py

@@ -0,0 +1,85 @@
+"""Replay post-battle transitions without a device or a 60-second watchdog wait."""
+from types import SimpleNamespace
+from unittest.mock import Mock
+
+import pytest
+
+from tasks.Exploration.base import Scene
+from tasks.Exploration.solo import SoloExploration
+
+
+class SceneReplay(SoloExploration):
+    def __init__(self, *frames):
+        self.frames = iter(frames)
+        self.frame = set()
+        self.clicks = []
+        self.device = SimpleNamespace(click_record_remove=Mock())
+        self.interval_timer = {}
+
+    def screenshot(self):
+        frame = next(self.frames, None)
+        assert frame is not None, 'Exploration kept waiting after the last replay frame'
+        self.frame = set(frame)
+
+    def appear(self, asset, **kwargs):
+        return any(asset is getattr(self, name) for name in self.frame)
+
+    def appear_then_click(self, asset, **kwargs):
+        if self.appear(asset):
+            self.clicks.append(asset)
+            return True
+        return False
+
+
+@pytest.mark.parametrize('marker,scene', [
+    ('I_E_EXPLORATION_CLICK', Scene.ENTRANCE),
+    ('I_E_AUTO_ROTATE_ON', Scene.MAIN),
+    ('I_E_AUTO_ROTATE_OFF', Scene.MAIN),
+])
+def test_chapter_controls_take_precedence_over_world_marker(marker, scene):
+    task = SceneReplay(('I_CHECK_EXPLORATION', marker))
+    assert task.get_current_scene(reuse_screenshot=False) == scene
+
+
+@pytest.mark.parametrize('frame', [
+    ('I_E_SETTINGS_BUTTON', 'I_EXP_ARROW_LEFT'), ('I_E_EXPLORATION_CLICK', 'I_EXP_ARROW_LEFT'),
+    ('I_PREPARE_HIGHLIGHT', 'I_EXP_ARROW_LEFT'), ('I_BATTLE_INFO', 'I_EXP_ARROW_LEFT'),
+    ('I_GI_EMOJI_1', 'I_EXP_ARROW_LEFT'), ('I_CHECK_EXPLORATION',), (),
+])
+def test_world_check_yields_unready_frames_without_clicking(frame):
+    task = SceneReplay(frame)
+    assert task.wait_world_stable() is False
+    assert task.clicks == []
+
+
+def test_hidden_world_panel_can_be_opened_when_its_marker_is_not_visible():
+    task = SceneReplay(('I_EXP_ARROW_LEFT',), ('I_EXP_ARROW_LEFT',),
+                       ('I_CHECK_EXPLORATION', 'I_EXP_ARROW_RIGHT'))
+    assert task.get_current_scene(reuse_screenshot=False) == Scene.WORLD
+    assert task.wait_world_stable() is False
+    assert task.clicks == [task.I_EXP_ARROW_LEFT]
+    assert task.wait_world_stable() is True
+    assert task.clicks == [task.I_EXP_ARROW_LEFT]
+
+
+@pytest.mark.parametrize('mode', ['run_solo', 'run_leader'])
+def test_transient_world_after_battle_keeps_chapter_search_state(mode):
+    task = SceneReplay(('I_CHECK_EXPLORATION',), ('I_E_SETTINGS_BUTTON',),
+                       ('I_E_SETTINGS_BUTTON',),
+                       ('I_CHECK_EXPLORATION',), ('I_CHECK_EXPLORATION', 'I_EXP_ARROW_RIGHT'))
+    task.explore_init = True
+    task.swipe_cnt = 2
+    continued = []
+
+    def handle_chapter():
+        continued.append((task.explore_init, task.swipe_cnt))
+        return True
+
+    task._handle_treasure_box = handle_chapter
+    task.check_exit = Mock(return_value=True)
+    task.wait_until_stable = Mock()  # The leader also checks for a pending invite on exit.
+    getattr(task, mode)()
+    assert continued == [(True, 2)]
+    task.device.click_record_remove.assert_called_once_with(task.S_SWIPE_BACKGROUND_RIGHT.name)
+    task.check_exit.assert_called_once_with()
+    assert not task.explore_init and task.swipe_cnt == 0

BIN
tests/tasks/GameUi/fixtures/exploration_roof.png


+ 12 - 0
tests/tasks/GameUi/test_exploration_marker.py

@@ -2,9 +2,12 @@
 import json
 from pathlib import Path
 import unittest
+from unittest.mock import patch
 
 import numpy as np
+from PIL import Image
 
+from module.atom import image as image_module
 from module.atom.image import RuleImage
 from tasks.GameUi.assets import GameUiAssets
 
@@ -47,6 +50,15 @@ class ExplorationMarkerTests(unittest.TestCase):
     def test_blank_frame_does_not_classify_a_map(self):
         self.assertFalse(self.rule().match(np.full((720, 1280, 3), 100, dtype=np.uint8)))
 
+    def test_chapter_roof_is_not_a_world_map_marker(self):
+        # Crop at (1128, 124) from the 2026-11-13 09:23:23 timeout screenshot.
+        with Image.open(Path(__file__).with_name('fixtures') / 'exploration_roof.png') as crop:
+            frame = np.zeros((720, 1280, 3), dtype=np.uint8)
+            frame[124:124 + crop.height, 1128:1128 + crop.width] = np.asarray(crop)
+        for pad in (0, 2):
+            with self.subTest(pad=pad), patch.object(image_module, 'ROI_MATCH_PAD', pad):
+                self.assertFalse(self.rule().match(frame))
+
     def test_generated_asset_keeps_the_source_search_box(self):
         source = self.rule()
         self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.roi_back, source.roi_back)