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

Merge pull request #2007 from leovefy/fix/six-realms-island-entry

fix(SixRealms): 修复月之海岛屿漏识别和点击偏移
huangrunheng 1 неделя назад
Родитель
Сommit
7854d54b6c

+ 10 - 0
tasks/SixRealms/assets.py

@@ -102,6 +102,16 @@ class SixRealmsAssets:
 	I_EXIT_SIXREALMS = RuleImage(roi_front=(507.5,308,100,100), roi_back=(507.5,308,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_exit_sixrealms.png")
 	I_EXIT_SIXREALMS = RuleImage(roi_front=(507.5,308,100,100), roi_back=(507.5,308,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_exit_sixrealms.png")
 	# 选择一个式神加入征伐队伍 
 	# 选择一个式神加入征伐队伍 
 	I_SELECT_SHIKIGAMI_AND_CONFIRM = RuleImage(roi_front=(499,238,151,57), roi_back=(479,213,199,113), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_select_shikigami_and_confirm.png")
 	I_SELECT_SHIKIGAMI_AND_CONFIRM = RuleImage(roi_front=(499,238,151,57), roi_back=(479,213,199,113), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_select_shikigami_and_confirm.png")
+	# rest 
+	I_ISLAND_MARKER_101 = RuleImage(roi_front=(649,384,20,90), roi_back=(144,227,989,376), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_marker_101.png")
+	# mystery 
+	I_ISLAND_MARKER_102 = RuleImage(roi_front=(907,449,20,90), roi_back=(144,227,989,376), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_marker_102.png")
+	# chaos 
+	I_ISLAND_MARKER_103 = RuleImage(roi_front=(650,402,20,90), roi_back=(144,227,989,376), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_marker_103.png")
+	# battle 
+	I_ISLAND_MARKER_104 = RuleImage(roi_front=(649,381,20,90), roi_back=(144,227,989,376), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_marker_104.png")
+	# star 
+	I_ISLAND_MARKER_105 = RuleImage(roi_front=(649,372,20,90), roi_back=(144,227,989,376), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_marker_105.png")
 
 
 
 
 	# Ocr Rule Assets
 	# Ocr Rule Assets

BIN
tasks/SixRealms/gate1/gate1_island_marker_101.png


BIN
tasks/SixRealms/gate1/gate1_island_marker_102.png


BIN
tasks/SixRealms/gate1/gate1_island_marker_103.png


BIN
tasks/SixRealms/gate1/gate1_island_marker_104.png


BIN
tasks/SixRealms/gate1/gate1_island_marker_105.png


+ 45 - 0
tasks/SixRealms/gate1/image.json

@@ -169,5 +169,50 @@
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "选择一个式神加入征伐队伍"
     "description": "选择一个式神加入征伐队伍"
+  },
+  {
+    "itemName": "island_marker_101",
+    "imageName": "gate1_island_marker_101.png",
+    "roiFront": "649,384,20,90",
+    "roiBack": "144,227,989,376",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "rest"
+  },
+  {
+    "itemName": "island_marker_102",
+    "imageName": "gate1_island_marker_102.png",
+    "roiFront": "907,449,20,90",
+    "roiBack": "144,227,989,376",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "mystery"
+  },
+  {
+    "itemName": "island_marker_103",
+    "imageName": "gate1_island_marker_103.png",
+    "roiFront": "650,402,20,90",
+    "roiBack": "144,227,989,376",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "chaos"
+  },
+  {
+    "itemName": "island_marker_104",
+    "imageName": "gate1_island_marker_104.png",
+    "roiFront": "649,381,20,90",
+    "roiBack": "144,227,989,376",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "battle"
+  },
+  {
+    "itemName": "island_marker_105",
+    "imageName": "gate1_island_marker_105.png",
+    "roiFront": "649,372,20,90",
+    "roiBack": "144,227,989,376",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "star"
   }
   }
 ]
 ]

+ 61 - 36
tasks/SixRealms/moon_sea/map.py

@@ -1,12 +1,9 @@
-import cv2
 import re
 import re
-import numpy as np
-from numpy import uint8, fromfile
+from module.atom.click import RuleClick
 from module.base.timer import Timer
 from module.base.timer import Timer
 from module.exception import GameStuckError
 from module.exception import GameStuckError
 from module.logger import logger
 from module.logger import logger
 
 
-from tasks.base_task import BaseTask
 from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
 from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
 from tasks.SixRealms.assets import SixRealmsAssets
 from tasks.SixRealms.assets import SixRealmsAssets
 from tasks.SixRealms.common import MoonSeaType
 from tasks.SixRealms.common import MoonSeaType
@@ -14,6 +11,13 @@ from tasks.SixRealms.oas_ocr import StoneOcr
 
 
 
 
 class MoonSeaMap(MoonSeaSkills):
 class MoonSeaMap(MoonSeaSkills):
+    island_markers = {
+        MoonSeaType.island101: SixRealmsAssets.I_ISLAND_MARKER_101,
+        MoonSeaType.island102: SixRealmsAssets.I_ISLAND_MARKER_102,
+        MoonSeaType.island103: SixRealmsAssets.I_ISLAND_MARKER_103,
+        MoonSeaType.island104: SixRealmsAssets.I_ISLAND_MARKER_104,
+        MoonSeaType.island105: SixRealmsAssets.I_ISLAND_MARKER_105,
+    }
     map_ocr = StoneOcr(
     map_ocr = StoneOcr(
         roi=(0, 0, 1280, 720),
         roi=(0, 0, 1280, 720),
         area=(0, 0, 1280, 720),
         area=(0, 0, 1280, 720),
@@ -26,15 +30,44 @@ class MoonSeaMap(MoonSeaSkills):
     def contains_any_char(string, chars):
     def contains_any_char(string, chars):
         return not set(string).isdisjoint(set(chars))
         return not set(string).isdisjoint(set(chars))
 
 
-    def decide(self) -> tuple:
-        """
-        这个玩意,检测不是很准
-        @return: 岛屿的类型,剩余多少回合, (x,y, w, h)
-        """
+    def _marker_boxes(self, kind):
+        marker = self.island_markers[kind]
+        return [tuple(match[1:]) for match in marker.match_all_any(self.device.image)]
+
+    @staticmethod
+    def _nearby_marker(boxes, roi):
+        # 岛屿上下浮动。同类岛并排时,只跟踪原来那一列。
+        center_x = roi[0] + roi[2] / 2
+        nearest = min(boxes, key=lambda box: abs(box[0] + box[2] / 2 - center_x), default=None)
+        if nearest and abs(nearest[0] + nearest[2] / 2 - center_x) <= 60:
+            return nearest
+
+    def decide(self, prefer_store=False) -> tuple:
         self.screenshot()
         self.screenshot()
         if self.appear(self.I_BOSS_FIRE):
         if self.appear(self.I_BOSS_FIRE):
             # 最后的boss
             # 最后的boss
             return MoonSeaType.island106, 0, (0, 0, 0, 0)
             return MoonSeaType.island106, 0, (0, 0, 0, 0)
+        kind, rounds, roi = self._read_map_ocr()
+        candidates = {}
+        for island in self.island_markers:
+            if island == MoonSeaType.island101 and self.appear(self.I_M_STORE):
+                continue
+            boxes = self._marker_boxes(island)
+            if boxes:
+                candidates[island] = boxes
+        if prefer_store and kind != MoonSeaType.island101 and MoonSeaType.island101 in candidates:
+            kind = MoonSeaType.island101
+            roi = max(candidates[kind])
+        elif kind != MoonSeaType.island100:
+            roi = self._nearby_marker(candidates.get(kind, []), roi) or roi
+        elif candidates:
+            # OCR 未识别出岛型时,沿用从右向左的选岛顺序。
+            kind, roi = max(((island, max(boxes)) for island, boxes in candidates.items()),
+                            key=lambda candidate: candidate[1][0])
+        logger.info('Island type: {}, Residue: {}, ROI: {}'.format(kind, rounds, roi))
+        return kind, rounds, roi
+
+    def _read_map_ocr(self):
         isl_type = MoonSeaType.island100
         isl_type = MoonSeaType.island100
         isl_roi = (0, 0, 0, 0)
         isl_roi = (0, 0, 0, 0)
         isl_num = 0
         isl_num = 0
@@ -76,7 +109,6 @@ class MoonSeaMap(MoonSeaSkills):
             elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
             elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
                 isl_type = MoonSeaType.island105
                 isl_type = MoonSeaType.island105
                 isl_roi = x1, y1, w, h
                 isl_roi = x1, y1, w, h
-        logger.info('Island type: {}, Residue: {}, ROI: {}'.format(isl_type, isl_num, isl_roi))
         return isl_type, isl_num, isl_roi
         return isl_type, isl_num, isl_roi
 
 
     def enter_island(self, isl_type, isl_roi):
     def enter_island(self, isl_type, isl_roi):
@@ -106,14 +138,29 @@ class MoonSeaMap(MoonSeaSkills):
                 click_timer.reset()
                 click_timer.reset()
             logger.info('Entering island randomly')
             logger.info('Entering island randomly')
             return
             return
-        isl_roi = [isl_roi[0]-40, isl_roi[1] + 70, isl_roi[2] + 40, isl_roi[3]+40]
-        self.C_ISLAND_ENTER.roi_front = isl_roi
+        timeout = Timer(15).start()
+        click_timer = Timer(2.5)
         while 1:
         while 1:
             self.screenshot()
             self.screenshot()
-            if not self.in_main() and self.appear(self.I_BACK_EXIT):
+            on_map = self.in_main()
+            if not on_map and self.appear(self.I_BACK_EXIT):
                 break
                 break
-            if self.click(self.C_ISLAND_ENTER, interval=2.5):
+            if timeout.reached():
+                raise GameStuckError(f'Enter {isl_type.name} timeout')
+            if not on_map or not click_timer.reached():
                 continue
                 continue
+            click_timer.reset()
+            roi = self._nearby_marker(self._marker_boxes(isl_type), isl_roi)
+            if not roi:
+                kind, _, box = self._read_map_ocr()
+                if kind != isl_type or not self._nearby_marker([box], isl_roi):
+                    continue
+                roi = box
+            x, y, w, h = roi
+            # 以当帧标牌中心为基准,落点在下方 15~75 像素。
+            cx, cy = int(x + w / 2), int(y + h / 2) + 45
+            click_area = (cx - 30, cy - 30, 60, 60)
+            self.click(RuleClick(click_area, click_area, name='island_enter'))
         logger.info('Entering island')
         logger.info('Entering island')
         return
         return
 
 
@@ -141,28 +188,6 @@ class MoonSeaMap(MoonSeaSkills):
                 cnt_act += 1
                 cnt_act += 1
                 continue
                 continue
 
 
-    def entry_island_random(self, area: list = None):
-        """
-
-        :param area: 搁置
-        :return:
-        """
-        # if not isinstance(area, list):
-        #     area = list(area)
-        logger.info('Entry island randomly')
-        while 1:
-            self.screenshot()
-            if not self.in_main() and self.appear(self.I_BACK_EXIT):
-                break
-            if self.click(self.C_ISLAND_ENTER_4, interval=2):
-                continue
-            if self.click(self.C_ISLAND_ENTER_5, interval=2):
-                continue
-            if self.click(self.C_ISLAND_ENTER_6, interval=2):
-                continue
-        return
-
-
 if __name__ == '__main__':
 if __name__ == '__main__':
     from module.config.config import Config
     from module.config.config import Config
     from module.device.device import Device
     from module.device.device import Device

+ 1 - 6
tasks/SixRealms/moon_sea/moon_sea.py

@@ -72,12 +72,7 @@ class MoonSea(MoonSeaMap, MoonSeaL101, MoonSeaL102, MoonSeaL103, MoonSeaL104, Mo
                 else:
                 else:
                     self.activate_store()
                     self.activate_store()
                     self.wait_animate_stable(self.C_MAIN_ANIMATE_KEEP, timeout=3)
                     self.wait_animate_stable(self.C_MAIN_ANIMATE_KEEP, timeout=3)
-                    isl_type, isl_num, isl_roi = self.decide()
-                    # 文字检测不一定发现到宁息
-                    if isl_type != MoonSeaType.island101:
-                        logger.warning('OCR not found island101')
-                        logger.warning('Try to entry the island in the right randomly order')
-                        self.entry_island_random()
+                    isl_type, isl_num, isl_roi = self.decide(prefer_store=True)
 
 
             # 如果是boss
             # 如果是boss
             if isl_type == MoonSeaType.island106:
             if isl_type == MoonSeaType.island106:

+ 37 - 0
tests/test_six_realms_island_entry.py

@@ -56,6 +56,43 @@ class IslandEntryTests(unittest.TestCase):
         self.enter_unknown(task)
         self.enter_unknown(task)
         self.assertEqual(task.events, [])
         self.assertEqual(task.events, [])
 
 
+    def test_known_island_tracks_height_without_switching_columns(self):
+        task = OfflineMoonSea(enter_after=2)
+        left = (288, 280, 20, 90)
+        frames = [[(1040, 300, 20, 90), left],
+                  [(288, 320, 20, 90), (1040, 340, 20, 90)]]
+        with patch.object(task, "_marker_boxes", side_effect=frames), \
+                patch.object(task, "click", wraps=task.click) as click, \
+                patch("module.base.timer.time.time", side_effect=lambda: task.now):
+            task.enter_island(MoonSeaType.island104, left)
+        self.assertEqual([call.args[0].roi_front for call in click.call_args_list],
+                         [(268, 340, 60, 60), (268, 380, 60, 60)])
+
+    def test_decide_keeps_ocr_target_without_a_nearby_marker(self):
+        task = OfflineMoonSea()
+        roi = (288, 280, 22, 80)
+        for target, marker, prefer_store in (
+            (MoonSeaType.island104, MoonSeaType.island104, False),
+            (MoonSeaType.island104, MoonSeaType.island103, False),
+            (MoonSeaType.island101, MoonSeaType.island101, True),
+        ):
+            ocr = (target, 9, roi)
+            boxes = {marker: [(1040, 300, 20, 90)]}
+            with self.subTest(target=target, marker=marker, prefer_store=prefer_store), \
+                    patch.object(task, "_read_map_ocr", return_value=ocr), \
+                    patch.object(task, "_marker_boxes", side_effect=lambda kind: boxes.get(kind, [])):
+                self.assertEqual(task.decide(prefer_store=prefer_store), ocr)
+
+    def test_disappearing_marker_times_out_without_clicking_adjacent_island(self):
+        task = OfflineMoonSea()
+        adjacent = (290, 330, 20, 90)
+        with patch.object(task, "_marker_boxes", return_value=[adjacent]), \
+                patch.object(task, "_read_map_ocr", return_value=(MoonSeaType.island104, 9, adjacent)), \
+                patch("module.base.timer.time.time", side_effect=lambda: task.now):
+            with self.assertRaisesRegex(GameStuckError, "Enter island104 timeout"):
+                task.enter_island(MoonSeaType.island104, (1040, 310, 20, 90))
+        self.assertEqual(task.events, [])
+
     def test_entry_cycles_in_order_with_one_shared_interval(self):
     def test_entry_cycles_in_order_with_one_shared_interval(self):
         task = OfflineMoonSea(enter_after=8)
         task = OfflineMoonSea(enter_after=8)
         self.enter_unknown(task)
         self.enter_unknown(task)