Просмотр исходного кода

Merge upstream dev through 4c688865 and preserve local fixes

Integrate 17 upstream commits, including four merges. Adopt adaptive
exploration searches, filtered-UP target selection, background-based edge
detection and swipe-record cleanup. Include the bounded Duel setup wait
and the delay before inviting a second friend.

Keep the local bidirectional chapter selection and its OCR-miss recovery;
it already covers the upstream one-direction swipe correction. Preserve
the existing mail, UI template, raid, treasure and battle strategy fixes.

Fix an upstream omission in the exploration retry path: recheck the scene
and confirmation dialogs after the second swipe before declaring the map
boundary. Three replay cases reproduce the premature exit without this
guard and pass with it.

Validation: 306 pytest tests passed (tests plus FrogBoss unit tests), with
21 new cases for exploration search/interruptions, Duel setup timeout and
second-friend timing. Chapter selection remains unchanged by AST comparison.
All seven changed Python files parse and four production modules import.
No assets or metadata changed; git diff checks passed. No emulator run.
unknown 1 месяц назад
Родитель
Сommit
bcc2619815

+ 2 - 0
tasks/Component/GeneralInvite/general_invite.py

@@ -481,6 +481,8 @@ class GeneralInvite(BaseTask, GeneralInviteAssets):
             logger.warning('Invite friend 1 failed')
         # 如果是邀请第二个人
         if config.invite_number == InviteNumber.TWO:
+            # 确定邀请前等待一段时间,避免游戏弹出邀请过快
+            sleep(3)
             success = self.invite_friend(config.friend_2, config.find_mode)
             if not success:
                 logger.warning('Invite friend 2 failed')

+ 2 - 1
tasks/Duel/script_task.py

@@ -191,7 +191,8 @@ class ScriptTask(GameUi, GeneralBattle, SwitchSoul, DuelAssets, SwitchOnmyoji):
                 self.duel_exit_battle()
                 continue
             if ret is None and not battle_operated:  # 进行战斗前的操作
-                self.ui_click(self.O_D_HAND, self.O_D_AUTO, interval=0.8)
+                # https://github.com/runhey/OnmyojiAutoScript/pull/1934
+                self.ui_click(self.O_D_HAND, self.O_D_AUTO, interval=6, timeout=45)
                 self.green_mark(self.conf.duel_config.green_enable, self.conf.duel_config.green_mark)
                 battle_operated = True
                 self.reset_device('BATTLE_STATUS_S')

+ 7 - 6
tasks/Exploration/base.py

@@ -58,9 +58,9 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
     def _match_end(self):
         return RuleAnimate(self.I_SWIPE_END)
 
-    def is_swipe_unchanged(self, before_image, after_image, threshold: float = 20) -> bool:
-        # 对比滑屏前后同一块区域,变化很小说明地图大概率没有移动,已经滑到尽头。
-        swipe_check_roi = (1216, 86, 64, 225)
+    def is_swipe_unchanged(self, before_image, after_image, threshold: float = 15) -> bool:
+        # 对比滑屏前后中上部背景区域(避开顶部跑马灯、下方小怪挑战图标及最右侧浮动鬼火),变化很小说明地图已滑到尽头。
+        swipe_check_roi = (250, 140, 800, 90)
         before = self.I_SWIPE_END.corp(before_image, swipe_check_roi)
         after = self.I_SWIPE_END.corp(after_image, swipe_check_roi)
         diff = np.mean(np.abs(before.astype(np.int16) - after.astype(np.int16)))
@@ -336,11 +336,11 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
                     self.I_NORMAL_BATTLE_BUTTON.roi_front = roi_front
                     logger.info(f"Target locked: sword at {roi_front} (aligned with UP icon)")
                     return self.I_NORMAL_BATTLE_BUTTON
-            else:
-                # 没找到 UP 图标,返回 None 让外层逻辑去处理(滑动或退出)
-                return None
+            # 没找到 UP 图标或对应战斗图标,返回 None 让外层逻辑去处理(滑动或退出)
+            return None
 
         # 2. 如果是默认情况 (UpType.ALL),则只要有怪就打
+        self.I_NORMAL_BATTLE_BUTTON.roi_back = (0, 0, 1279, 719)
         if self.appear(self.I_NORMAL_BATTLE_BUTTON):
             return self.I_NORMAL_BATTLE_BUTTON
             
@@ -396,6 +396,7 @@ class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, Replace
 
     def quit_explore(self):
         logger.info('Quit explore')
+        self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
         boss_timer = Timer(15)
         boss_timer.start()
         # click_yellow_button = 0 #用于保证只点一次左上返回按钮,不要直接触发连点回到主界面

+ 106 - 20
tasks/Exploration/solo.py

@@ -18,7 +18,11 @@ class SoloExploration(BaseExploration):
     INVITE_FLAG_ON = (227, 193, 153)
     INVITE_TIMEOUT = 20
     FRIEND_LEAVE_TIMEOUT = 3
+    SEARCH_SWIPE_INTERVAL = 1.5
+    MAX_SWIPE_COUNT = 2
     explore_init = False
+    swipe_cnt: int = 0
+    search_fail_timer: Timer = None
 
     @cached_property
     def _invite_config(self) -> InviteConfig:
@@ -60,9 +64,71 @@ class SoloExploration(BaseExploration):
         self.quit_explore()
         return True
 
+    def _reset_search_state(self, reset_swipe_cnt: bool = False) -> None:
+        if reset_swipe_cnt:
+            if self.swipe_cnt > 0:
+                logger.info(f"Exploration search reset (cleared count: {self.swipe_cnt})")
+            self.swipe_cnt = 0
+        self.search_fail_timer.clear()
+
+    def _search_next_area(self) -> None:
+        if not self.search_fail_timer.started():
+            self.search_fail_timer.start()
+            return
+        if not self.search_fail_timer.reached():
+            return
+
+        # 若选择特定 UP(如经验怪),达到最大滑屏次数(默认2次,覆盖前3屏高密度区)超时仍无目标直接早退重开;
+        # 若选择“全部”,则不限次数滑动直到最右边界,以保证清完全图小怪并击杀 BOSS。
+        is_specific_up = self._config.exploration_config.up_type != UpType.ALL
+        if is_specific_up and self.swipe_cnt >= self.MAX_SWIPE_COUNT:
+            logger.info(f"Max search swipe count ({self.MAX_SWIPE_COUNT}) reached for specific UP target. Quitting exploration early.")
+            self._reset_search_state(reset_swipe_cnt=True)
+            self.quit_explore()
+            return
+
+        before_image = self.device.image
+        self.swipe(self.S_SWIPE_BACKGROUND_RIGHT)
+        sleep(0.3)
+        self.screenshot()
+        unchanged = self.is_swipe_unchanged(before_image, self.device.image)
+        if unchanged:
+            sleep(0.3)
+            self.screenshot()
+            unchanged = self.is_swipe_unchanged(before_image, self.device.image)
+
+        if (self.get_current_scene() != Scene.MAIN
+                or self.appear(self.I_UI_CONFIRM)
+                or self.appear(self.I_UI_CONFIRM_SAMLL)):
+            self.search_fail_timer.reset()
+            return
+
+        # 若未产生位移,原地做快速二次复核保底
+        if unchanged:
+            sleep(0.3)
+            before_retry = self.device.image
+            self.swipe(self.S_SWIPE_BACKGROUND_RIGHT)
+            sleep(0.3)
+            self.screenshot()
+            # 复核滑动也可能触发战斗或弹窗,交回主循环处理,不能据此判断地图边界。
+            if (self.get_current_scene() != Scene.MAIN
+                    or self.appear(self.I_UI_CONFIRM)
+                    or self.appear(self.I_UI_CONFIRM_SAMLL)):
+                self.search_fail_timer.reset()
+                return
+            if self.is_swipe_unchanged(before_retry, self.device.image):
+                logger.info("Reached rightmost boundary (confirmed twice). Quitting exploration.")
+                self._reset_search_state(reset_swipe_cnt=True)
+                self.quit_explore()
+                return
+
+        self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
+        self.swipe_cnt += 1
+        self.search_fail_timer.reset()
+
     def run_solo(self):
         logger.hr('solo')
-        search_fail_timer = Timer(3.2)  # 这里设置的时间一定要大于S_SWIPE_BACKGROUND_RIGHT滑动的时间
+        self.search_fail_timer = Timer(self.SEARCH_SWIPE_INTERVAL)
 
         while 1:
             self.screenshot()
@@ -70,6 +136,9 @@ class SoloExploration(BaseExploration):
             logger.info(f'[run_solo] Current scene: {scene.name}')  # TODO 2026.06.22 之后删掉这个刷屏的
             #
             if scene == Scene.WORLD:
+                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
@@ -79,6 +148,9 @@ class SoloExploration(BaseExploration):
                 continue
             #
             elif scene == Scene.ENTRANCE:
+                self.explore_init = False
+                self._reset_search_state(reset_swipe_cnt=True)
+                self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                 if self.check_exit():
                     break
                 self.ui_click(self.I_E_EXPLORATION_CLICK, stop=self.I_E_SETTINGS_BUTTON)
@@ -90,37 +162,38 @@ class SoloExploration(BaseExploration):
                         self.enter_settings_and_do_operations()
                     self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON, timeout=5)
                     self.explore_init = True
+                    self._reset_search_state(reset_swipe_cnt=True)
                     continue
                 if self._handle_treasure_box():
+                    self._reset_search_state()
                     continue
                 if (self.appear_then_click(self.I_UI_CONFIRM, interval=1)
                         or self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1)):
+                    self._reset_search_state()
                     continue
                 # 小纸人
                 if self.appear(self.I_BATTLE_REWARD):
                     if self.ui_get_reward(self.I_BATTLE_REWARD):
+                        self._reset_search_state()
                         continue
                 # boss
                 if self.appear(self.I_BOSS_BATTLE_BUTTON):
+                    self._reset_search_state()
+                    self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                     if self.fire(self.I_BOSS_BATTLE_BUTTON):
                         logger.info(f'Boss battle, minions cnt {self.minions_cnt}')
                     continue
                 # 小怪
                 fight_button = self.search_up_fight()
                 if fight_button is not None:
+                    self._reset_search_state()
+                    self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                     if self.fire(fight_button):
                         logger.info(f'Fight, minions cnt {self.minions_cnt}')
                     continue
                 # 向后拉,寻找怪
-                if search_fail_timer.reached():
-                    search_fail_timer.reset()
-                    if self._match_end.stable(self.device.image, refresh_after_stable=True):
-                        self.quit_explore()
-                        continue
-                    self.swipe(self.S_SWIPE_BACKGROUND_RIGHT, interval=3)
-                    continue
-                if not search_fail_timer.started():
-                    search_fail_timer.start()
+                self._search_next_area()
+                continue
             #
             elif scene == Scene.BATTLE_PREPARE or scene == Scene.BATTLE_FIGHTING:
                 self.check_take_over_battle(is_screenshot=False, config=self._config.general_battle_config)
@@ -129,7 +202,7 @@ class SoloExploration(BaseExploration):
 
     def run_leader(self):
         logger.hr('leader')
-        search_fail_timer = Timer(3.2)  # 这里设置的时间一定要大于S_SWIPE_BACKGROUND_RIGHT滑动的时间
+        self.search_fail_timer = Timer(self.SEARCH_SWIPE_INTERVAL)
         friend_leave_timer = Timer(self.FRIEND_LEAVE_TIMEOUT)
 
         while 1:
@@ -137,6 +210,9 @@ class SoloExploration(BaseExploration):
             scene = self.get_current_scene()
             # 探索大世界
             if scene == Scene.WORLD:
+                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
@@ -155,6 +231,9 @@ class SoloExploration(BaseExploration):
 
             # 邀请好友, 非常有可能是后面邀请好友,然后直接跳到组队了
             elif scene == Scene.ENTRANCE:
+                self.explore_init = False
+                self._reset_search_state(reset_swipe_cnt=True)
+                self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                 while 1:
                     self.screenshot()
                     if self.is_in_room():
@@ -170,6 +249,8 @@ class SoloExploration(BaseExploration):
                         continue
             #
             elif scene == Scene.TEAM:
+                self.explore_init = False
+                self._reset_search_state(reset_swipe_cnt=True)
                 self.wait_until_stable(self.I_ADD_2, timer=Timer(0.8, 1))
                 if self.appear(self.I_FIRE, threshold=0.8) and not self.appear(self.I_ADD_2):
                     self.ui_click_until_disappear(self.I_FIRE, interval=1)
@@ -196,37 +277,42 @@ class SoloExploration(BaseExploration):
                         self.enter_settings_and_do_operations()
                     self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON)
                     self.explore_init = True
+                    self._reset_search_state(reset_swipe_cnt=True)
                     continue
                 if self._handle_treasure_box():
+                    self._reset_search_state()
+                    continue
+                if (self.appear_then_click(self.I_UI_CONFIRM, interval=1)
+                        or self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1)):
+                    self._reset_search_state()
                     continue
                 # 小纸人
                 if self.appear(self.I_BATTLE_REWARD):
                     if self.ui_get_reward(self.I_BATTLE_REWARD):
+                        self._reset_search_state()
                         continue
                 # 中途有人跑路
                 if self._check_mate_leave(friend_leave_timer):
+                    self._reset_search_state()
                     continue
                 # boss
                 if self.appear(self.I_BOSS_BATTLE_BUTTON):
+                    self._reset_search_state()
+                    self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                     if self.fire(self.I_BOSS_BATTLE_BUTTON):
                         logger.info(f'Boss battle, minions cnt {self.minions_cnt}')
                     continue
                 # 小怪
                 fight_button = self.search_up_fight()
                 if fight_button is not None:
+                    self._reset_search_state()
+                    self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
                     if self.fire(fight_button):
                         logger.info(f'Fight, minions cnt {self.minions_cnt}')
                     continue
                 # 向后拉,寻找怪
-                if search_fail_timer.reached():
-                    search_fail_timer.reset()
-                    if self._match_end.stable(self.device.image, refresh_after_stable=True):
-                        self.quit_explore()
-                        continue
-                    self.swipe(self.S_SWIPE_BACKGROUND_RIGHT, interval=3)
-                    continue
-                if not search_fail_timer.started():
-                    search_fail_timer.start()
+                self._search_next_area()
+                continue
             #
             elif scene == Scene.BATTLE_PREPARE or scene == Scene.BATTLE_FIGHTING:
                 self.check_take_over_battle(is_screenshot=False, config=self._config.general_battle_config)

+ 26 - 0
tests/tasks/Component/GeneralInvite/test_invite_timing.py

@@ -7,6 +7,7 @@ from module.base import timer
 from tasks.base_task import BaseTask
 from tasks.BondlingFairyland.general_invite import GeneralInvite as BondlingInvite
 from tasks.Component.GeneralInvite.general_invite import GeneralInvite
+from tasks.Component.GeneralInvite.config_invite import InviteConfig, InviteNumber
 
 
 class InviteReplay(BaseTask):
@@ -66,3 +67,28 @@ def test_delayed_invitation_redraw_does_not_repeat_clicks(monkeypatch, invite, c
     assert task.device.image == set()
     expected = [task.I_UI_UNCHECK.name] if checkbox == 'unchecked' else []
     assert task.clicks == [*expected, task.I_UI_CONFIRM.name]
+
+
+@pytest.mark.parametrize('number', [InviteNumber.ONE, InviteNumber.TWO])
+def test_second_friend_is_invited_after_the_first_dialog_settles(monkeypatch, number):
+    now = [0.0]
+    invitations = []
+
+    def sleep(seconds):
+        now[0] += seconds
+
+    def invite_friend(name, find_mode):
+        invitations.append((name, now[0]))
+        return True
+
+    monkeypatch.setattr('tasks.Component.GeneralInvite.general_invite.sleep', sleep)
+    config = InviteConfig(invite_number=number, friend_1='first', friend_2='second')
+    GeneralInvite.invite_friends(SimpleNamespace(invite_friend=invite_friend), config)
+
+    assert invitations[0] == ('first', 0.0)
+    if number == InviteNumber.TWO:
+        assert len(invitations) == 2
+        assert invitations[1][0] == 'second'
+        assert invitations[1][1] >= 3
+    else:
+        assert len(invitations) == 1

+ 69 - 0
tests/tasks/Duel/test_setup_wait.py

@@ -0,0 +1,69 @@
+from types import SimpleNamespace
+
+import pytest
+
+from module.atom.image import RuleImage
+from module.atom.ocr import RuleOcr
+from module.base import timer
+from tasks.Duel.script_task import ScriptTask
+
+
+class DuelReplay(ScriptTask):
+    def __init__(self, monkeypatch, auto_after):
+        self.now = 100.0
+        self.frames = 0
+        self.interval_timer = {}
+        self.clicks = []
+        self.auto_after = auto_after
+        self.setup_finished = False
+        self.resets = []
+        self.conf = SimpleNamespace(duel_config=SimpleNamespace(green_enable=False, green_mark=None))
+        self.device = SimpleNamespace(image=set(), click=self.record_click)
+        monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
+        monkeypatch.setattr(RuleImage, 'match', lambda asset, image, **kw: asset.name in image)
+        monkeypatch.setattr(RuleOcr, 'ocr', lambda asset, image, **kw: asset.keyword if asset.name in image else '')
+
+    def screenshot(self):
+        self.frames += 1
+        assert self.frames < 70, 'Duel setup never returned to the battle loop'
+        self.now += 1
+        if self.setup_finished:
+            self.device.image = {self.I_CHECK_DUEL.name, self.I_D_HELP.name}
+        else:
+            self.device.image = {self.O_D_HAND.name}
+            if self.auto_after is not None and len(self.clicks) >= self.auto_after:
+                self.device.image.add(self.O_D_AUTO.name)
+
+    def record_click(self, x, y, control_name):
+        assert control_name == self.O_D_HAND.name
+        self.clicks.append(self.now)
+
+    def check_and_get_reward(self):
+        pass
+
+    def is_battle_win(self):
+        return False
+
+    def is_battle_lose(self):
+        return False
+
+    def green_mark(self, *args):
+        self.setup_finished = True
+
+    def reset_device(self, reason):
+        self.resets.append(reason)
+
+
+@pytest.mark.parametrize('auto_after', [None, 1], ids=['timeout', 'auto-appears'])
+def test_setup_returns_to_battle_loop_with_bounded_clicks(monkeypatch, auto_after):
+    task = DuelReplay(monkeypatch, auto_after)
+    assert task.wait_battle() is None
+    assert task.setup_finished
+    assert task.resets == ['BATTLE_STATUS_S']
+    if auto_after is None:
+        assert 45 <= task.now - 100 <= 48
+        assert 1 < len(task.clicks) <= 8
+        assert all(later - earlier >= 6 for earlier, later in zip(task.clicks, task.clicks[1:]))
+    else:
+        assert task.now - 100 < 10
+        assert len(task.clicks) == 1

+ 152 - 0
tests/tasks/Exploration/test_search.py

@@ -0,0 +1,152 @@
+from copy import deepcopy
+from types import SimpleNamespace
+
+import numpy as np
+import pytest
+
+from module.base import timer
+from module.base.timer import Timer
+from tasks.Exploration.base import BaseExploration, Scene
+from tasks.Exploration.config import UpType
+from tasks.Exploration.solo import SoloExploration
+
+
+def frame(value=0, scene=Scene.MAIN, popup=None):
+    return value, scene, popup
+
+
+class SearchReplay(SoloExploration):
+    def __init__(self, monkeypatch, frames, up_type=UpType.ALL):
+        self._config = SimpleNamespace(exploration_config=SimpleNamespace(up_type=up_type))
+        self.now = 100.0
+        self.frames = iter(frames)
+        self.scene = Scene.MAIN
+        self.popup = None
+        self.swipes = []
+        self.removed = []
+        self.exits = 0
+        self.swipe_cnt = 0
+        self.device = SimpleNamespace(
+            image=np.zeros((720, 1280, 3), dtype=np.uint8),
+            click_record_remove=self.removed.append,
+        )
+        monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
+        monkeypatch.setattr('tasks.Exploration.solo.sleep', lambda _: None)
+        self.search_fail_timer = Timer(self.SEARCH_SWIPE_INTERVAL)
+
+    def screenshot(self):
+        value, self.scene, self.popup = next(self.frames)
+        self.device.image = np.full((720, 1280, 3), value, dtype=np.uint8)
+
+    def swipe(self, asset):
+        self.swipes.append(asset.name)
+
+    def get_current_scene(self):
+        return self.scene
+
+    def appear(self, asset):
+        return asset is self.popup
+
+    def quit_explore(self):
+        self.exits += 1
+
+    def search(self):
+        self._search_next_area()
+        self.now += 2
+        self._search_next_area()
+
+
+def test_search_waits_for_no_target_interval(monkeypatch):
+    task = SearchReplay(monkeypatch, [frame(80)])
+    task._search_next_area()
+    task.now += 1.4
+    task._search_next_area()
+    assert task.swipes == []
+    task.now += 0.2
+    task._search_next_area()
+    assert len(task.swipes) == task.swipe_cnt == 1
+
+
+@pytest.mark.parametrize('up_type', [UpType.EXP, UpType.COIN, UpType.DARUMAA])
+def test_specific_up_restarts_after_two_search_swipes(monkeypatch, up_type):
+    task = SearchReplay(monkeypatch, [], up_type)
+    task.swipe_cnt = 2
+    task.search()
+    assert task.exits == 1
+    assert task.swipe_cnt == 0
+    assert not task.search_fail_timer.started()
+    assert task.swipes == []
+
+
+def test_all_targets_continues_past_two_swipes(monkeypatch):
+    task = SearchReplay(monkeypatch, [frame(80)])
+    task.swipe_cnt = 2
+    task.search()
+    assert task.exits == 0
+    assert task.swipe_cnt == 3
+    assert task.removed == [task.S_SWIPE_BACKGROUND_RIGHT.name]
+
+
+def test_boundary_requires_a_second_swipe(monkeypatch):
+    task = SearchReplay(monkeypatch, [frame(), frame(), frame()])
+    task.search()
+    assert len(task.swipes) == 2
+    assert task.exits == 1
+
+
+@pytest.mark.parametrize('frames,swipes', [([frame(), frame(80)], 1),
+                                         ([frame(), frame(), frame(80)], 2)])
+def test_delayed_or_initially_ignored_swipe_does_not_exit(monkeypatch, frames, swipes):
+    task = SearchReplay(monkeypatch, frames)
+    task.search()
+    assert len(task.swipes) == swipes
+    assert task.swipe_cnt == 1
+    assert task.exits == 0
+
+
+@pytest.mark.parametrize('retry', [False, True])
+@pytest.mark.parametrize('interruption', ['battle', 'confirm', 'small-confirm'])
+def test_swipe_interruption_returns_to_scene_handling(monkeypatch, retry, interruption):
+    task = SearchReplay(monkeypatch, [])
+    scene = Scene.BATTLE_PREPARE if interruption == 'battle' else Scene.MAIN
+    popup = {'battle': None, 'confirm': task.I_UI_CONFIRM,
+             'small-confirm': task.I_UI_CONFIRM_SAMLL}[interruption]
+    frames = [frame(), frame(), frame(0, scene, popup)] if retry else [frame(80, scene, popup)]
+    task.frames = iter(frames)
+    task.search()
+    assert task.exits == 0, 'A battle or popup must not be treated as the map boundary'
+    assert task.swipe_cnt == 0
+    assert len(task.swipes) == (2 if retry else 1)
+
+
+def test_swipe_comparison_ignores_floating_effects_but_detects_background_motion():
+    task = object.__new__(BaseExploration)
+    before = np.zeros((720, 1280, 3), dtype=np.uint8)
+    effects = before.copy()
+    effects[86:311, 1216:1280] = 255
+    assert task.is_swipe_unchanged(before, effects)
+    background = before.copy()
+    background[140:230, 250:1050] = 80
+    assert not task.is_swipe_unchanged(before, background)
+
+
+def test_specific_up_without_aligned_sword_does_not_attack_another_monster(monkeypatch):
+    task = object.__new__(BaseExploration)
+    task.device = SimpleNamespace(image=None)
+    task.I_NORMAL_BATTLE_BUTTON = deepcopy(task.I_NORMAL_BATTLE_BUTTON)
+    monkeypatch.setattr(task.I_NORMAL_BATTLE_BUTTON, 'match_all', lambda **kwargs: [])
+    monkeypatch.setattr(task, 'appear', lambda asset: asset in (task.I_UP_EXP, task.I_NORMAL_BATTLE_BUTTON))
+    assert task.search_up_fight(UpType.EXP) is None
+
+
+def test_all_targets_restores_full_map_search(monkeypatch):
+    task = object.__new__(BaseExploration)
+    task.I_NORMAL_BATTLE_BUTTON = deepcopy(task.I_NORMAL_BATTLE_BUTTON)
+    task.I_NORMAL_BATTLE_BUTTON.roi_back = (100, 100, 80, 80)
+
+    def appear(asset):
+        assert asset.roi_back == (0, 0, 1279, 719)
+        return True
+
+    monkeypatch.setattr(task, 'appear', appear)
+    assert task.search_up_fight(UpType.ALL) is task.I_NORMAL_BATTLE_BUTTON